//
//  SendDetailTableViewCell.m
//  DriverUse
//
//  Created by angra on 16/11/2.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "SendDetailTableViewCell.h"
#import "HomeDetailContentModel.h"
#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface SendDetailTableViewCell()
@property (nonatomic,weak)UILabel *userLabel;
@property (nonatomic,weak)UILabel *phoneLabel;
@property (nonatomic,weak)UILabel *personCountLabel;
@property (nonatomic,weak)UILabel *typeLabel;
@property (nonatomic,weak)UILabel *startLabel;
@property (nonatomic,weak)UILabel *endLabel;
@property(nonatomic,retain)NSString *phoneNumber;
@end

@implementation SendDetailTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = kColoeString(@"f7f7f7");
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setInterface];
    }
    return self;
    
}
-(void)setInterface
{
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = kWhiteColor;
    [self.contentView addSubview:backView];
    
    UILabel *userLabel = [self createLabelWithText:@"拼车用户：李彦鹏"];
    [backView addSubview:userLabel];
    _userLabel = userLabel;
    
    UILabel *phoneLabel = [self createLabelWithText:@"联系方式：19999999999"];
    [backView addSubview:phoneLabel];
    _phoneLabel = phoneLabel;
    
    UILabel *personCountLabel = [self createLabelWithText:@"拼车人数：2人"];
    [backView addSubview:personCountLabel];
    _personCountLabel = personCountLabel;
    
    UILabel *typeLabel = [self createLabelWithText:@"支付状态：司机代付"];
    [backView addSubview:typeLabel];
    _typeLabel = typeLabel;
    
    UILabel *startLabel = [self createLabelWithText:@"上车地点：长沙市芙蓉区解放西路"];
    [backView addSubview:startLabel];
    _startLabel = startLabel;
    
    UILabel *endLabel = [self createLabelWithText:@"下车地点：湘潭市东槐镇北五区"];
    [backView addSubview:endLabel];
    _endLabel = endLabel;
    
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kScaleNum(10));
        make.top.equalTo(self.contentView).offset(kScaleNum(5));
        make.right.equalTo(self.contentView).offset(kScaleNum(-10));
        make.bottom.equalTo(self.contentView);
    }];
    [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(kScaleNum(15));
        make.top.equalTo(backView).offset(kScaleNum(10));
        make.width.lessThanOrEqualTo(@(kScreenFrameW-kScaleNum(90)));
    }];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userLabel);
        make.top.equalTo(userLabel.mas_bottom).offset(kScaleNum(4));
    }];
    [personCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userLabel);
        make.top.equalTo(phoneLabel.mas_bottom).offset(kScaleNum(4));
    }];
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userLabel);
        make.top.equalTo(personCountLabel.mas_bottom).offset(kScaleNum(4));
    }];
  
    
    
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneBtn addTarget:self
                 action:@selector(phoneBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [phoneBtn setImage:[UIImage imageNamed:@"dial"] forState:UIControlStateNormal];
    [backView addSubview:phoneBtn];
    
    
    UIButton *startLocation = [UIButton buttonWithType:UIButtonTypeCustom];
    [startLocation addTarget:self
                 action:@selector(startLocationBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [startLocation setImage:[UIImage imageNamed:@"localtion"] forState:UIControlStateNormal];
    [backView addSubview:startLocation];
    
    UIButton *endLocation = [UIButton buttonWithType:UIButtonTypeCustom];
    [endLocation addTarget:self
                 action:@selector(endLocationBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [endLocation setImage:[UIImage imageNamed:@"localtion"] forState:UIControlStateNormal];
    [backView addSubview:endLocation];
    
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backView).offset(-kScaleNum(10));
        make.top.equalTo(backView).offset(kScaleNum(10));
 
    }];
    
    [startLocation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backView).offset(-kScaleNum(0));
        make.centerY.equalTo(startLabel);
        make.width.equalTo(@30);
  
    }];
    
    [endLocation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backView).offset(-kScaleNum(0));
        make.centerY.equalTo(endLabel);
        make.width.equalTo(@30);
    }];
    
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userLabel);
        make.top.equalTo(typeLabel.mas_bottom).offset(kScaleNum(4));
        make.right.equalTo(startLocation).offset(-kScaleNum(10));
    
    }];
    [endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userLabel);
        make.top.equalTo(startLabel.mas_bottom).offset(kScaleNum(4));
        make.right.equalTo(endLocation).offset(-kScaleNum(10));
       
    }];
    
    
    
}
-(UILabel *)createLabelWithText:(NSString *)text
{
    UILabel *label = [[UILabel alloc]init];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font(kScaleNum(12))];
    label.textColor = kColoeString(@"666666");
    return label;
}


-(void)phoneBtnClick
{
//点击电话标识，对用户直接呼叫
    if ([self.delegate respondsToSelector:@selector(clickPhoneWith:)]) {
        [self.delegate clickPhoneWith:self.phoneNumber];
    }
    
}

-(void)startLocationBtnClick
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *gaodeMapAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self navigationWithType:0];
        
    }];
    UIAlertAction *baiduMapAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self navigationWithType:1];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:gaodeMapAction];
    [alertVC addAction:baiduMapAction];
    [alertVC addAction:cancleAction];
    [self.fatherVC presentViewController:alertVC animated:YES completion:^{
        
    }];

}
-(void)endLocationBtnClick
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *gaodeMapAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self navigationWithType:0];
        
    }];
    UIAlertAction *baiduMapAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self navigationWithType:1];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:gaodeMapAction];
    [alertVC addAction:baiduMapAction];
    [alertVC addAction:cancleAction];
    [self.fatherVC presentViewController:alertVC animated:YES completion:^{
        
    }];

}
-(void)navigationWithType:(NSInteger)type{
    __block MKMapItem *goLocation;
    __block MKMapItem *toLocation;
    __block CLLocationCoordinate2D goCoordinate2d;
    __block CLLocationCoordinate2D toCoordinate2d;
    //上车地点，直接跳转高德或者百度地图进行导航
    [[LocationManager shareInstance] getCoordinateByAddress:_model.origin regionReturn:^(CLLocationCoordinate2D coordinate) {
        goCoordinate2d = coordinate;
        goLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
        if (goLocation && toLocation) {
            if (type == 0) {//高德地图
                dispatch_async(dispatch_get_main_queue(), ^{
                    [MKMapItem openMapsWithItems:@[goLocation, toLocation]
                                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
                                                   MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
                });
            }
            if (type == 1) {//百度地图
                NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|title:出发点&destination=latlng:%f,%f|title:终点&mode=driving&coord_type=bd09ll";
                NSString *urlString = [[NSString stringWithFormat:baiduParameterFormat,
                                        goCoordinate2d.latitude,
                                        goCoordinate2d.longitude,
                                        toCoordinate2d.latitude,
                                        toCoordinate2d.longitude]
                                       stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
                }else{
                    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:@"没有下载百度地图" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    [alertVC addAction:sureAction];
                    [self.fatherVC presentViewController:alertVC animated:YES completion:^{
                        
                    }];
                    
                }
            }
        }
    }];
    
    [[LocationManager shareInstance] getCoordinateByAddress:_model.destination regionReturn:^(CLLocationCoordinate2D coordinate) {
        toCoordinate2d = coordinate;
        toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
        if (goCoordinate2d.latitude && goCoordinate2d.longitude && toCoordinate2d.latitude && toCoordinate2d.longitude) {
        }
        if (goLocation && toLocation) {
            if (type == 0) {//高德地图
                dispatch_async(dispatch_get_main_queue(), ^{
                    [MKMapItem openMapsWithItems:@[goLocation, toLocation]
                                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
                                                   MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
                });
            }
            if (type == 1) {//百度地图
                NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|title:出发点&destination=latlng:%f,%f|title:终点&mode=driving&coord_type=bd09ll";
                NSString *urlString = [[NSString stringWithFormat:baiduParameterFormat,
                                        goCoordinate2d.latitude,
                                        goCoordinate2d.longitude,
                                        toCoordinate2d.latitude,
                                        toCoordinate2d.longitude]
                                       stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
                }else{
                    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:@"没有下载百度地图" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    [alertVC addAction:sureAction];
                    [self.fatherVC presentViewController:alertVC animated:YES completion:^{
                        
                    }];
                    
                }
            }
        }
    }];

}
-(void)setModel:(HomeDetailContentModel *)model
{
    _model = model;
    _userLabel.text = [NSString stringWithFormat:@"拼车用户：%@",model.cname] ;
    _typeLabel.text = [NSString stringWithFormat:@"支付状态：%@",model.pay_status] ;
    _startLabel.text = [NSString stringWithFormat:@"上车地点：%@",model.origin] ;
    _endLabel.text = [NSString stringWithFormat:@"下车地点：%@",model.destination] ;
    _phoneLabel.text = [NSString stringWithFormat:@"联系方式：%@",model.phone] ;
    _personCountLabel.text = [NSString stringWithFormat:@"拼车人数：%@",model.number] ;
    self.phoneNumber =model.phone;

}

@end
